package com.party.orders.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.party.domain.Coupon;
import com.party.dto.Page;
import com.party.exception.ExceptionMsgEnum;
import com.party.exception.ServiceException;
import com.party.orders.mapper.CouponMapper;
import com.party.orders.service.CouponService;
/**
 * 
 * @author Caizhf
 * @date 2017年7月29日下午3:23:49
 * @version v.0.1
 * @email 1115054416@qq.com 
 *
 * <p>Description: </p>
 *
 */
@Service
public class CouponServiceImpl implements CouponService{

	@Autowired
	private CouponMapper mapper;
	
	@Override
	public void save(Coupon coupon) {
		if(mapper.save(coupon)==null)
			throw new ServiceException(ExceptionMsgEnum.SERVER_ERROR_MSG);
	}

	@Override
	public void deleteById(int cpid) {
		if(mapper.deleteById(cpid)==null)
			throw new ServiceException(ExceptionMsgEnum.SERVER_ERROR_MSG);
	}

	@Override
	public void update(Coupon coupon) {
		if(mapper.updateById(coupon)==null)
			throw new ServiceException(ExceptionMsgEnum.SERVER_ERROR_MSG);
	}

	@Override
	public Coupon findById(int cpid) {
		Coupon cp = mapper.findById(cpid);
		return cp;
	}

	@Override
	public Page list(int pageNum, int pageSize, String key, Integer userId, Boolean isOverdue) {
		int totleRecord = 0;
		Page page = null;
		List<Coupon> list=null;
		//如果需要查过期的劵
		if(isOverdue!=null){
			totleRecord = mapper.countList(key,userId,new Date(System.currentTimeMillis()),isOverdue);
			page = new Page(totleRecord,pageNum,pageSize);
			list = mapper.list(key, userId, page.getStartindex(), pageSize,new Date(System.currentTimeMillis()), isOverdue);
		}
		//如果不需要查过期的劵
		else{
			totleRecord = mapper.countList(key,userId,null,null);
			page = new Page(totleRecord,pageNum,pageSize);
			list = mapper.list(key, userId, page.getStartindex(), pageSize, null, null);
		}
		page.setList(list);
		return page;
	}

}
